// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;
using svs.Mobile.BeyondMedia.FeedCore;
using svs.Mobile.BeyondMedia.Properties;
using svs.Mobile.BeyondMedia.UiCore;

namespace svs.Mobile.BeyondMedia.Objects.RSS
{
	internal static class SMSMessageInterceptor
	{
		private const string APP_ID = "BeyondPod";
		private const string KEY = "BeyondPodRSS:";
		private static MessageInterceptor _SMSCatcher = null;

		public static void StartAppLaunchInterceptor()
		{
			if (MessageInterceptor.IsApplicationLauncherEnabled(APP_ID))
			{
				_SMSCatcher = new MessageInterceptor(APP_ID);
			}
			else
			{
				BuildInterceptor();
				_SMSCatcher.EnableApplicationLauncher(APP_ID, Assembly.GetExecutingAssembly().GetName().CodeBase);
			}

			if (_SMSCatcher != null)
				_SMSCatcher.MessageReceived += OnMessageReceived;
		}

		public static void StopAppLaunchInterceptor()
		{
			if (MessageInterceptor.IsApplicationLauncherEnabled(APP_ID))
			{
				_SMSCatcher = new MessageInterceptor(APP_ID);
				_SMSCatcher.DisableApplicationLauncher();
			}
		}

		public static void StartSessionInterception()
		{
			BuildInterceptor();
			if (_SMSCatcher != null)
				_SMSCatcher.MessageReceived += OnMessageReceived;
		}

		private static void BuildInterceptor()
		{
			_SMSCatcher = new MessageInterceptor(InterceptionAction.NotifyAndDelete);
			_SMSCatcher.MessageCondition =
				new MessageCondition(MessageProperty.Body, MessagePropertyComparisonType.Contains, KEY, false);
		}

		private static void OnMessageReceived(object sender, MessageInterceptorEventArgs e)
		{
			SmsMessage mySMS = (SmsMessage) e.Message;
			ImportFeeds(mySMS.Body);
		}

		private static void ImportFeeds(string body)
		{
			int index = body.IndexOf(KEY, StringComparison.InvariantCultureIgnoreCase);
			string feedstring = body.Substring(index + KEY.Length);

			char[] dels = {',', ';', '|'};

			string[] feeds = feedstring.Split(dels);

			int imported = 0;
			foreach (string feed in feeds)
			{
				string trimmed = feed.Trim();

				if (FeedRepository.GetFeedByUrl(trimmed) != null)
				{
					MessageBoxEx.Show(string.Format(Resources.SMSImportFeedExistsMsg, trimmed), Resources.SMSImportFeedExistsTitle);
					continue;
				}

				Feed f = new Feed(Guid.NewGuid(), FeedRepository.RootFeed);
				f.FeedUrl = trimmed;
				f.Name = trimmed;

				FeedRepository.RootFeed.Feeds.Add(f);
				imported++;
			}

			MessageBoxEx.Show(string.Format(Resources.SMSImportCompleteMsg, imported), Resources.SMSImportCompleteTitle);
		}
	}
}